package org.jeecg.modules.demo.yyc.setting.salelink.service.impl;

import org.jeecg.modules.demo.yyc.setting.salelink.entity.YycSaleLink;
import org.jeecg.modules.demo.yyc.setting.salelink.entity.YycSaleLinkGoods;
import org.jeecg.modules.demo.yyc.setting.salelink.mapper.YycSaleLinkGoodsMapper;
import org.jeecg.modules.demo.yyc.setting.salelink.mapper.YycSaleLinkMapper;
import org.jeecg.modules.demo.yyc.setting.salelink.service.IYycSaleLinkService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 销售链接
 * @Author: jeecg-boot
 * @Date:   2023-10-09
 * @Version: V1.0
 */
@Service
public class YycSaleLinkServiceImpl extends ServiceImpl<YycSaleLinkMapper, YycSaleLink> implements IYycSaleLinkService {

	@Autowired
	private YycSaleLinkMapper yycSaleLinkMapper;
	@Autowired
	private YycSaleLinkGoodsMapper yycSaleLinkGoodsMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(YycSaleLink yycSaleLink, List<YycSaleLinkGoods> yycSaleLinkGoodsList) {
		yycSaleLinkMapper.insert(yycSaleLink);
		if(yycSaleLinkGoodsList!=null && yycSaleLinkGoodsList.size()>0) {
			for(YycSaleLinkGoods entity:yycSaleLinkGoodsList) {
				//外键设置
				entity.setSaleLinkId(yycSaleLink.getId());
				yycSaleLinkGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(YycSaleLink yycSaleLink,List<YycSaleLinkGoods> yycSaleLinkGoodsList) {
		yycSaleLinkMapper.updateById(yycSaleLink);
		
		//1.先删除子表数据
		yycSaleLinkGoodsMapper.deleteByMainId(yycSaleLink.getId());
		
		//2.子表数据重新插入
		if(yycSaleLinkGoodsList!=null && yycSaleLinkGoodsList.size()>0) {
			for(YycSaleLinkGoods entity:yycSaleLinkGoodsList) {
				//外键设置
				entity.setSaleLinkId(yycSaleLink.getId());
				yycSaleLinkGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		yycSaleLinkGoodsMapper.deleteByMainId(id);
		yycSaleLinkMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			yycSaleLinkGoodsMapper.deleteByMainId(id.toString());
			yycSaleLinkMapper.deleteById(id);
		}
	}
	
}
